// import store from '../../store/index.js'
import {
	showToast
} from '../utils.js'
import {
	login,
} from '../api/user.js'
import store from '../../store/index.js'
import {
	baseUrl
} from '../const.js'
/**
 * 接口请求地址
 * 
 */
// var baseUrl = 'http://114.55.146.93:9091/api' // 测试地址
// var baseUrl = 'http://172.16.21.98:8081/api' // 谭静地址
// var baseUrl = 'https://ymlab.cmlabs.com.cn/api' // 正式地址

/**
 * 全局请求方法
 * url： 接口地址
 * data： 传给接口的数据
 * method: POST,GET
 * contentType: 请求头
 */

export const request = (url, data, method, contentType) => {
	return new Promise((resolve, reject) => {
		let header = {
			"Content-Type": contentType || "application/x-www-form-urlencoded;charset=utf-8",
			"Authorization": "Basic c2FiZXI6c2FiZXJfc2VjcmV0"
		}
		if (uni.getStorageSync('token')) {
			header['Blade-Auth'] = 'bearer ' + uni.getStorageSync('token') // 让每个请求携带token--['Authorization']为自定义key 请根据实际情况自行修改
		}
		uni.request({
			url: baseUrl + url, //接口地址
			data: {
				...data,
			},
			dataType: 'text',
			method: method,
			header: header,
			responseType: 'text',
			success: (res) => {
				var resData = JSON.parse(res.data)
				if (resData.error_description) {
					// showToast(resData.error_description)
					resData && reject(resData) // 返回错误
				} else {
					if ((!resData.error_description || resData.code == '200') && resData.code != '400' && resData.code != '500' &&
						resData.code != '401') {
						resData && resolve(resData) // 返回数据
					} else {
						if (resData.code == '401') {
							console.log(resData)
							// showToast('登录超时，请重新登录')
							// uni.reLaunch({
							// 	url: '/pages/login/login'
							// });
							// 如果登录超时 重新调登录接口 获取最新的token
							toLogin(url, data, method, contentType).then(res=>{
								resolve(res)
							})
						} else {
							showToast(resData.msg || resData.message)
							resData && reject(resData)
						}
						
					}
				}
			},
			fail: (err) => {
				// showToast(`微信请求错误：${JSON.stringify(err)}，请稍后重试～`) // request:fail
				err && reject(err)
			}
		});
	})
}

// 登录
function toLogin(url, data, method, contentType) {
	console.log('重新登录')
	return new Promise((resolve, reject) => {
		let params = {
			openId: store.state.openId,
			grant_type: 'social'				
		}
		login(params).then(res => {
			uni.setStorageSync('token', res.access_token);
			request(url, data, method, contentType).then(res=>{
				resolve(res)
			})
		})
	})
}

// token
function postToken(url, data, method, contentType) {
	console.log('======token=========')
	return new Promise((resolve, reject) => {
				let params = {
					username: 'admin',
					password: '21232f297a57a5a743894a0e4a801fc3',
					tenantId: '000000',
					grant_type: 'password',
					scope: "all",
					type: 'account'
				}
		getToken(params).then(res => {
			store.commit('setAccountInfo',res)
			uni.setStorageSync('accountInfo', res)
			uni.setStorageSync('token', res.access_token);
			request(url, data, method, contentType).then(res=>{
				resolve(res)
			})
		})
	})
}


